package JET.graphics;

import JET.Centers.ControlCenter;
import JET.geom.Sec2d;
import JET.geom.Vec2d;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

/**
 *
 * @author 057Ry
 */
public class GraphicHelper
{
    static final ControlCenter CC = ControlCenter.getInstance();

    public static Rectangle convertRectFromEngineToScreenSystem(GraphicPanel gp, Rectangle engRect)
    {
        int x1 = (int) gp.engineToScrX( engRect.getMinX() );
        int y1 = (int) gp.engineToScrY( engRect.getMaxY() );

        int x2 = (int) gp.engineToScrX( engRect.getMaxX() );
        int y2 = (int) gp.engineToScrY( engRect.getMinY() );

        return new Rectangle(x1,y1, x2-x1,y2-y1);
    }

    public static void drawArrow(Graphics2D g2d, Vec2d v1, Vec2d v2, double k, boolean drawCircle) {
        Sec2d sec = new Sec2d(v1, v2);

        double kat = sec.getAngleY();
        double len = sec.getLength();
        if ( len > 0 )
        {
            Vec2d top = new Vec2d( 0, len );
            Vec2d left = new Vec2d( -3*k, len - 6*k );
            Vec2d right = new Vec2d( 3*k, len - 6*k );
            top.rotate(kat);
            left.rotate(kat);
            right.rotate(kat);

            double x1 =  v1.getX() ;
            double y1 =  v1.getY() ;
            double xTop = v1.getX()+top.getX() ;
            double yTop =  v1.getY()+top.getY() ;
            double xLeft =  v1.getX()+left.getX() ;
            double yLeft =  v1.getY()+left.getY() ;
            double xRight =  v1.getX()+right.getX() ;
            double yRight =  v1.getY()+right.getY() ;

            //g2d.setColor(Color.ORANGE);
            if (drawCircle)
                g2d.draw( new Ellipse2D.Double(x1-2*k, y1-2*k, 4*k, 4*k) );


            g2d.draw( new Line2D.Double(x1, y1, xTop, yTop) );
            g2d.draw( new Line2D.Double(xLeft, yLeft, xTop, yTop) );
            g2d.draw( new Line2D.Double(xRight, yRight, xTop, yTop) );
        }
    }

    public static void drawArrow(Graphics2D g2d, Vec2d v1, Vec2d v2, double k)
    {
        drawArrow(g2d, v1, v2, k, true);
    }
    
    public static void drawArrow(Graphics2D g2d, double x1, double y1, double x2, double y2, double k) {
        drawArrow(g2d, new Vec2d(x1,y1), new Vec2d(x2,y2), k, true);
    }
}
